<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="索引" prop="idx">
        <el-input
          v-model="queryParams.idx"
          placeholder="请输入索引"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-input
          v-model="queryParams.icon"
          placeholder="请输入图标"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样式" prop="cls">
        <el-input
          v-model="queryParams.cls"
          placeholder="请输入样式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置(inner-内部 top-顶部 bottom-底部)" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入位置(inner-内部 top-顶部 bottom-底部)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="只读显示" prop="readonlyShow">
        <el-input
          v-model="queryParams.readonlyShow"
          placeholder="请输入只读显示"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="弹框视图" prop="actionDialogViewId">
        <el-input
          v-model="queryParams.actionDialogViewId"
          placeholder="请输入弹框视图"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="弹框应用" prop="actionDialogAppId">
        <el-input
          v-model="queryParams.actionDialogAppId"
          placeholder="请输入弹框应用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动作成功" prop="actionSuccess">
        <el-input
          v-model="queryParams.actionSuccess"
          placeholder="请输入动作成功"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="selectType" prop="selectType">
        <el-select v-model="queryParams.selectType" placeholder="请选择selectType" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="动作失败" prop="actionFailure">
        <el-input
          v-model="queryParams.actionFailure"
          placeholder="请输入动作失败"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示条件" prop="showIf">
        <el-input
          v-model="queryParams.showIf"
          placeholder="请输入显示条件"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="视图ID" prop="viewId">
        <el-input
          v-model="queryParams.viewId"
          placeholder="请输入视图ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rva:viewbutton:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rva:viewbutton:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rva:viewbutton:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['rva:viewbutton:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="viewbuttonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="索引" align="center" prop="idx" />
      <el-table-column label="图标" align="center" prop="icon" />
      <el-table-column label="样式" align="center" prop="cls" />
      <el-table-column label="位置(inner-内部 top-顶部 bottom-底部)" align="center" prop="position" />
      <el-table-column label="只读显示" align="center" prop="readonlyShow" />
      <el-table-column label="动作(dialog-弹框 ajax-后端调用 js-前端调用)" align="center" prop="action" />
      <el-table-column label="动作Url" align="center" prop="actionUrl" />
      <el-table-column label="弹框视图" align="center" prop="actionDialogViewId" />
      <el-table-column label="弹框应用" align="center" prop="actionDialogAppId" />
      <el-table-column label="动作成功" align="center" prop="actionSuccess" />
      <el-table-column label="selectType" align="center" prop="selectType" />
      <el-table-column label="动作失败" align="center" prop="actionFailure" />
      <el-table-column label="显示条件" align="center" prop="showIf" />
      <el-table-column label="视图ID" align="center" prop="viewId" />
      <el-table-column label="其他数据" align="center" prop="data" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rva:viewbutton:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rva:viewbutton:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视图属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="索引" prop="idx">
          <el-input v-model="form.idx" placeholder="请输入索引" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入图标" />
        </el-form-item>
        <el-form-item label="样式" prop="cls">
          <el-input v-model="form.cls" placeholder="请输入样式" />
        </el-form-item>
        <el-form-item label="位置(inner-内部 top-顶部 bottom-底部)" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置(inner-内部 top-顶部 bottom-底部)" />
        </el-form-item>
        <el-form-item label="只读显示" prop="readonlyShow">
          <el-input v-model="form.readonlyShow" placeholder="请输入只读显示" />
        </el-form-item>
        <el-form-item label="动作(dialog-弹框 ajax-后端调用 js-前端调用)" prop="action">
          <el-input v-model="form.action" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="动作Url" prop="actionUrl">
          <el-input v-model="form.actionUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="弹框视图" prop="actionDialogViewId">
          <el-input v-model="form.actionDialogViewId" placeholder="请输入弹框视图" />
        </el-form-item>
        <el-form-item label="弹框应用" prop="actionDialogAppId">
          <el-input v-model="form.actionDialogAppId" placeholder="请输入弹框应用" />
        </el-form-item>
        <el-form-item label="动作成功" prop="actionSuccess">
          <el-input v-model="form.actionSuccess" placeholder="请输入动作成功" />
        </el-form-item>
        <el-form-item label="selectType" prop="selectType">
          <el-select v-model="form.selectType" placeholder="请选择selectType">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="动作失败" prop="actionFailure">
          <el-input v-model="form.actionFailure" placeholder="请输入动作失败" />
        </el-form-item>
        <el-form-item label="显示条件" prop="showIf">
          <el-input v-model="form.showIf" placeholder="请输入显示条件" />
        </el-form-item>
        <el-form-item label="视图ID" prop="viewId">
          <el-input v-model="form.viewId" placeholder="请输入视图ID" />
        </el-form-item>
        <el-form-item label="其他数据" prop="data">
          <el-input v-model="form.data" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listViewbutton, getViewbutton, delViewbutton, addViewbutton, updateViewbutton, exportViewbutton } from "@/api/rva/viewbutton";

export default {
  name: "Viewbutton",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视图属性表格数据
      viewbuttonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        idx: null,
        icon: null,
        cls: null,
        position: null,
        readonlyShow: null,
        action: null,
        actionUrl: null,
        actionDialogViewId: null,
        actionDialogAppId: null,
        actionSuccess: null,
        selectType: null,
        actionFailure: null,
        showIf: null,
        viewId: null,
        data: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        viewId: [
          { required: true, message: "视图ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询视图属性列表 */
    getList() {
      this.loading = true;
      listViewbutton(this.queryParams).then(response => {
        this.viewbuttonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        idx: null,
        icon: null,
        cls: null,
        position: null,
        readonlyShow: null,
        action: null,
        actionUrl: null,
        actionDialogViewId: null,
        actionDialogAppId: null,
        actionSuccess: null,
        selectType: null,
        actionFailure: null,
        showIf: null,
        viewId: null,
        data: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视图属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getViewbutton(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改视图属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateViewbutton(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addViewbutton(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除视图属性编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delViewbutton(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有视图属性数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportViewbutton(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
